package main  
  
import (  
    "fmt"  
    "io/ioutil"  
    "log"  
    "net/http"  
	
	)  
  
func main() {  
    // 读取静态 HTML 文件  
    htmlData, err := ioutil.ReadFile("index.html")  
    if err != nil {  
        log.Fatal(err)  
    }  
  
    // 定义一个处理函数来返回静态文件内容  
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {  
        // 设置响应头为 text/html  
        w.Header().Set("Content-Type", "text/html")  
        // 写入 HTML 数据到响应体  
        fmt.Fprint(w, string(htmlData))  
    })
	
	http.HandleFunc("/",handleGetRequest)


  
    // 启动 HTTP 服务器，监听端口 8080  
    fmt.Println("Starting server on :8080")  
    if err := http.ListenAndServe(":8080", nil); err != nil {  
        log.Fatal(err)  
    }  
}

func handleGetRequest(w http.ResponseWriter, r *http.Request) { 

	if r.Method != http.MethodGet{
		http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)  
		return  
	}
	// 解析查询参数  
	query := r.URL.Query()  
	name := query.Get("name")  
	age := query.Get("age")  

	// 构建响应消息  
	response := fmt.Sprintf("Hello, %s! Your age is %s.", name, age)  

	// 写入响应头  
	w.Header().Set("Content-Type", "text/plain;charset=utf-8")  

	// 写入响应体  
	fmt.Fprint(w, response)  


}